<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/16
 * Time: 10:46
 * Desc: 门店商品推荐 -- 推荐商品分类 -- 推荐商品
 */
namespace app\store\controller;

use controller\BasicAdmin;

use service\DataService;

use think\Db;

class Recommend extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    推荐商品分类 -- 列表
     */
//    public function index(){
//        $this->title = '推荐分类';
//        //门店id
//        $store_id = session('user')['store_id'];
//        $get = $this->request->get();
//        $where_category = [
//            'a.status'=>1,
//            'a.is_deleted'=>0,
//            'a.is_exp'=>0
//        ];
//
//        $db = Db::name('goods_category')->alias('a')->join('(select * from ys_store_recommend_category where is_deleted=0 and store_id='.$store_id.') b','a.id=b.cate_id','LEFT')->where($where_category);
//        foreach (['title'] as $key) {
//            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
//        }
//        $db->field('a.*,ifnull(b.id,0) as is_recommend');
//        $this->assign('store_id',$store_id);
//        return parent::_list($db);
//    }
    /**
     * @Author  lingyun
     * @Desc    推荐门店首页商品分类
     */
    public function index(){
        $this->title = '推荐分类';
        $store_id = session('user')['store_id'];
        $where_recommend_1 = [
            'a.store_id'=>$store_id,
            'a.is_deleted'=>0,
            'a.p_id'=>1
        ];
        $where_recommend_2 = [
            'a.store_id'=>$store_id,
            'a.is_deleted'=>0,
            'a.p_id'=>2
        ];
        //推荐的分类
        $recommend_category_1 = db('store_recommend_category')->alias('a')->join('ys_goods_category b','a.cate_id=b.id','LEFT')->where($where_recommend_1)->field('a.*,b.title')->find();
        $recommend_category_2 = db('store_recommend_category')->alias('a')->join('ys_goods_category b','a.cate_id=b.id','LEFT')->where($where_recommend_2)->field('a.*,b.title')->find();
        //商品分类
        $where_category = [
            'is_exp'=>0,
            'is_deleted'=>0,
            'status'=>1
        ];
        //商品分类
        $category = db('goods_category')->where($where_category)->select();
        $this->assign('store_id',$store_id);
        $this->assign('recommend_category_1',$recommend_category_1);
        $this->assign('recommend_category_2',$recommend_category_2);
        $this->assign('category',$category);
        $this->assign('title','推荐分类');
        return $this->fetch('index');
    }

    /**
     * @Author  lingyun
     * @Desc    设置推荐按商品分类
     */
    public function set_recommend_category(){
        $request = request();
        $store_id = session('user')['store_id'];
        if($request->isPost()){
            $post = input('post.');
            $where = [
                'store_id'=>$store_id,
                'p_id'=>$post['p_id']
            ];
            //查询是否存在推荐商品分类
            $recommend_category = db('store_recommend_category')->where($where)->find();
            if(!empty($recommend_category)){
                $res = db('store_recommend_category')->where('id',$recommend_category['id'])->update(['cate_id'=>$post['cate_id']]);
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }else{
                $post['addtime'] = time();
                $post['store_id'] = $store_id;
                $post['is_deleted'] = 0;
                $res = db('store_recommend_category')->data($post)->insert();
                $res>0?$this->success('保存成功',''):$this->error('保存失败','');
            }
        }else{
            //推荐分类id
            $id = input('id');
            //推荐商品分类位
            $p_id = input('p_id');
            $where_category = [
                'is_exp'=>0,
                'is_deleted'=>0
            ];
            //可以推荐的商品分类列表
            $category = db('goods_category')->where($where_category)->select();
            //当前选中的推荐分类
            $cur_category = db('store_recommend_category')->where(['store_id'=>$store_id,'id'=>$id,'p_id'=>$p_id])->find();
            $this->assign('cur_category',$cur_category);
            $this->assign('category',$category);
            $this->assign('id',$id);
            $this->assign('p_id',$p_id);
            $this->assign('title','设置推荐分类');
            return $this->fetch();
        }
    }

    /**
     * @Author  lingyun
     * @Desc    编辑推荐商品分类信息
     */
    public function edit_recommend_category(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
//            halt($post);
            $res = db('store_recommend_category')->update($post);
            $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
        }
        $id = input('id');
        $info = db('store_recommend_category')->where('id',$id)->find();
//        halt($info);
        $this->assign('info',$info);
        $this->assign('id',$id);
        return $this->fetch('edit_recommend_category');
    }

    /**
     * @Author  lingyun
     * @Desc    设置推荐商品分类
     */
//    public function set_recommend_category(){
//        $request = request();
//        //门店id
//        $store_id = session('user')['store_id'];
//        $where = [
//            'store_id'=>$store_id,
//            'is_deleted'=>0
//        ];
//        $recommend_list = db('store_recommend_category')->where($where)->select();
//        $post = input('post.');
//        $where_recommend = [
//            'store_id'=>$store_id,
//            'cate_id'=>$post['id']
//        ];
//        $post = input('post.');
//        //门店id
//        $store_id = session('user')['store_id'];
//        //推荐分类
//        if($post['value'] == 0){
//
//            if(count($recommend_list)>=2){
//                $this->error('最多推荐分类不能超过2种','');
//            }else{
//                //查询记录，判断数据表种是否有记录
//                $record = db('store_recommend_category')->where($where_recommend)->find();
//                if(!empty($record)){
//                    $res = db('store_recommend_category')->where($where_recommend)->update(['is_deleted'=>$post['value']]);
//                    $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
//                }else{
//                    $data = [
//                        'store_id'=>$store_id,
//                        'cate_id'=>$post['id'],
//                        'is_deleted'=>0,
//                        'addtime'=>time()
//                    ];
//                    $res = db('store_recommend_category')->data($data)->insert();
//                    $res>0?$this->success('设置成功',''):$this->error('设置失败','');
//                }
//            }
//        }else{
//            $res = db('store_recommend_category')->where($where_recommend)->update(['is_deleted'=>$post['value']]);
//            $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
//        }
//    }

    /**
     * @Author  lingyun
     * @Desc    推荐商品列表
     */
    public function recommend_goods_list(){
        $this->title = '推荐商品';
        $id = input('id');                              //门店推荐分类商品主键id
        $get = $this->request->get();
        $store_id = session('user')['store_id'];        //门店id
        $where_recommend_goods = [
            'a.store_id'=>$store_id,
            'a.cate_id'=>$id,
            'a.is_deleted'=>0,
        ];
        $join = [
            ['ys_store_goods_stock c','a.goods_id=c.id','LEFT'],
            ['ys_goods b','c.goods_id=b.id','LEFT']
        ];
        $db = Db::name('store_recommend_goods')->alias('a')->join($join)->where($where_recommend_goods);
        foreach (['title'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.title', "%{$get[$key]}%");
        }
        $db->field('a.*,b.name');
        $qiniu = config('qiniu');
        $this->assign('yangshi', $qiniu['yangshi']);          //七牛样式后缀
        $this->assign('store_id',$store_id);
        $this->assign('cate_id',$id);       //分类id
        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    添加 -- 编辑推荐商品
     */
    public function add_edit_goods(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            //查询推荐的商品数量，最多不能超过4个
            $count = db('store_recommend_goods')->where(['store_id'=>$post['store_id'],'status'=>1,'is_deleted'=>0])->count();
            if(!empty($post['id'])){
                $res = db('store_recommend_goods')->data($post)->update();
                $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
            }else{
//                if($count>=4){
//                    if($post['status'] == 1){
//                        $this->error('推荐商品最多不能超过4个','');
//                    }
//                }
                $post['addtime'] = time();
                $res = db('store_recommend_goods')->data($post)->insert();
                $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
            }
        }
        $id = input('id');
        $cate_id = input('cate_id');            //商品分类id
        //门店id
        $store_id = session('user')['store_id'];
        $join = [
            ['ys_goods b','a.goods_id=b.id','LEFT']
        ];
        //推荐的信息
        $recommend = db('store_recommend_goods')->alias('a')->join($join)->where('a.id',$id)->field('a.*,b.name')->find();
        $where_goods = [
            'a.is_deleted'=>0,
            'a.onsale'=>1,
            'a.store_id'=>$store_id,
            'b.cat_id'=>$cate_id
        ];
        $goods = db('store_goods_stock')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_goods)->field('b.name,b.logo,a.id')->select();
        $this->assign('store_id',$store_id);
        $this->assign('cate_id',$cate_id);
        $this->assign('goods',$goods);
        $this->assign('recommend',$recommend);
        return $this->fetch('add_edit_goods');
    }

    /**
     * @Author  lingyun
     * @Desc    设置推荐商品状态
     */
    public function set_recommend_goods_status(){
        $post = input('post.');
        $res = db('store_recommend_goods')->where('id',$post['id'])->update([$post['field']=>$post['value']]);
        $res!==false?$this->success('设置成功',''):$this->error('设置失败','');
    }

    /**
     * @Author  lingyun
     * @Desc    删除推荐商品
     */
    public function del_recomend_goods(){
        $table = 'StoreRecommendGoods';
        if (DataService::update($table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}